﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic.CustomerManager;
using PayrollHR.Web;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;

namespace PayrollHR.Administration.Modules
{
    public partial class CustomerSearchControl : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int UserId = PayrollContext.Current.Session.EmployeeId;
                hfdUserId.Value = CommonHelper.ToString(UserId);
                this.FillCustType();
                this.Bind();
            }
        }
        private void Bind()
        {
            var customerSearch = this.CustomerService.GetCustomerSearch(PayrollContext.Current.Session.EmployeeId);
            if (!CommonHelper.IsNull(customerSearch))
            {
                ddlCustType.SelectedValue = customerSearch.CutomerTypeId.ToString();
                txtCustEmail.Text = customerSearch.CustEmail;
                txtTaxcode.Text = customerSearch.TaxCode;
                txtCustomerName.Text = customerSearch.CustName;
                txtMobile.Text = customerSearch.CustMobile;
            }
        }
        public void FillCustType()
        {
            ddlCustType.Items.Clear();
            var customertypeSelection = this.CustomerService.GetCustomerType();
            customertypeSelection.Insert(0, new CutomerType { CutomerTypeId = 0, CutomerTypeName = "---All---" });
            foreach (var customertype in customertypeSelection)
            {
                ListItem ddlCustTypeItem2 = new ListItem(customertype.CutomerTypeName, customertype.CutomerTypeId.ToString());
                ddlCustType.Items.Add(ddlCustTypeItem2);
            }
        }
        public CustomerSearchResult GetCustomerSearch()
        {
            int customerTypeId = 0;
            if (ddlCustType.SelectedIndex > -1)
            {
                customerTypeId = int.Parse(ddlCustType.SelectedItem.Value);
            }
            CustomerSearchResult customer = new CustomerSearchResult
            {
                CustName = txtCustomerName.Text.Trim(),
                CutomerTypeId = customerTypeId,
                CustMobile = txtMobile.Text.Trim(),
                CustEmail = txtCustEmail.Text.Trim(),
                TaxCode = txtTaxcode.Text.Trim(),
                UserId = CommonHelper.ToInteger(hfdUserId.Value)
            };            
            return customer;
        }
    }
}